//
//  VideoViewController.m
//  LeagueOfLegend
//
//  Created by appying_wxj on 14/12/17.
//  Copyright (c) 2014年 appying_wxj. All rights reserved.
//

#import "VideoViewController.h"
#import "MJRefresh.h"
#import "VideoTableViewCell.h"
#import "UIImageView+ASIRequest.h"
#import "UIImageView+WebCache.h"
#import "VideoDetailViewController.h"
#import "VideoDataObject.h"
@interface VideoViewController ()

@end

@implementation VideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.titleLab.text = @"最新视频";
    
    _videoArray = [[NSMutableArray alloc] initWithCapacity:0];
    _videoDataArray = [[NSMutableArray alloc] initWithCapacity:0];

    [self initVideoUI];
    [self getVideoDate];
    start = 1;
    
}

// 创建界面
-(void)initVideoUI{
    _videoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64-49) style:UITableViewStylePlain];
    _videoTableView.delegate = self;
    _videoTableView.dataSource = self;
    _videoTableView.rowHeight = 70;
    [self.view addSubview:_videoTableView];
    [self setExtraCellLineHidden:self.videoTableView];
    
    // 2.集成刷新控件
    [self setupRefresh];
}
// 获取数据
-(void)getVideoDate{

    
    [_videoArray removeAllObjects];

    
    NSString *reqStr = [NSString stringWithFormat:@"http://lol.video.luckyamy.com/api/?cat=home&page=%d",start];
    NSString *getStr = [Request requestData:reqStr];
    _videoArray = [Request JsonData:getStr];
//    NSLog(@"------------>%@",_videoArray);
    for (NSDictionary *dic in _videoArray)
    {
        VideoDataObject *video = [[VideoDataObject alloc] initWithDictionar:dic];
        [_videoDataArray addObject:video];
    }
    
    [_videoTableView reloadData];
    [_videoTableView headerEndRefreshing];// 下拉刷新
    [_videoTableView footerEndRefreshing];// 上提加载
}
//// 上提加载
//-(void)pageLoadData{
//    
////    NSString *url = [NSString stringWithFormat:@"%@&page=%d",HTTPURL,start];
////    NSString *getStr = [Request requestData:url];
////    _videoArray = [Request JsonData:getStr];
//    
//    [self getVideoDate];
//}
#pragma mark ASIHTTPRequest
-(void)requestFailed:(ASIHTTPRequest *)request{
    [SVProgressHUD showErrorWithStatus:@"网络稍慢或服务器请求异常,请稍后再试~^_^" duration:2];
    [_videoTableView headerEndRefreshing];
    [_videoTableView footerEndRefreshing];
    
}

#pragma mark-------下拉刷新-------
//隐藏表格多余分割线
- (void)setExtraCellLineHidden: (UITableView *)tableView {
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [tableView setTableHeaderView:view];
}
/**
 为了保证内部不泄露，在dealloc中释放占用的内存
 */
- (void)dealloc {
//    NSLog(@"MJTableViewController--dealloc---");
}
/**
 *  集成刷新控件
 */
- (void)setupRefresh {
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.videoTableView addHeaderWithTarget:self action:@selector(headerRereshing)];
//#warning 自动刷新(一进入程序就下拉刷新)
    [self.videoTableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.videoTableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.videoTableView.headerPullToRefreshText = @"下拉可以刷新了";
    self.videoTableView.headerReleaseToRefreshText = @"松开马上刷新了";
    self.videoTableView.headerRefreshingText = @"正在刷新中";
    
    self.videoTableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.videoTableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    self.videoTableView.footerRefreshingText = @"正在加载中";
}

#pragma mark 开始进入刷新状态
// 下拉刷新
- (void)headerRereshing {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        start = 1;
        [self getVideoDate];

    });
}
// 上提加载
- (void)footerRereshing {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        start ++;
        [self getVideoDate];
    });
}

#pragma mark----UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.videoDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static  NSString *cellIdentifier = @"Cell";
    //    根据重用标示符从表格中出列一个可以重用的单元格
    VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        
        cell = [[VideoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    else{  //重要，释放cell,防止闪退方法，其他CustomCell 均可沿用
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    
    VideoDataObject *video = [self.videoDataArray objectAtIndex:indexPath.row];
    [cell layoutVideoCell:video];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VideoDetailViewController *detail = [[VideoDetailViewController alloc] init];
    
    VideoDataObject *video = [self.videoDataArray objectAtIndex:indexPath.row];
    detail.detailStr = video.url;
    [self.navigationController pushViewController:detail animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
